package com.hwhhhh.mygrad.fragment;

import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.google.gson.reflect.TypeToken;
import com.hwhhhh.mygrad.R;
import com.hwhhhh.mygrad.api.Api;
import com.hwhhhh.mygrad.api.ApiCallback;
import com.hwhhhh.mygrad.api.ApiConfig;
import com.hwhhhh.mygrad.dto.CommentDTO;
import com.hwhhhh.mygrad.entity.RespData;
import com.hwhhhh.mygrad.interfaces.CommentListener;
import com.hwhhhh.mygrad.utils.StringUtils;

import java.util.HashMap;

public class CommentFragment extends DialogFragment {
    private View rootView;
    private TextView tvReplyTo;
    private EditText etFragmentComment;
    private Button btnFragmentComment;
    private int clsId, parentId;
    private String text;
    private CommentListener commentListener;

    public CommentFragment(int clsId, int parentId, String text) {
        this.clsId = clsId;
        this.parentId = parentId;
        this.text = text;
    }

    public static CommentFragment getNewInstance(int clsId, int parentId, String text) {
        return new CommentFragment( clsId, parentId, text);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_comment, container, false);
        initView();
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    @Override
    public void onStart() {
        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;//设置宽度为铺满
        params.gravity = Gravity.BOTTOM;
        getDialog().getWindow().setAttributes(params);
        super.onStart();
    }

    private void initView() {
        etFragmentComment = rootView.findViewById(R.id.et_fragment_comment);
        btnFragmentComment = rootView.findViewById(R.id.btn_fragment_comment);
        tvReplyTo = rootView.findViewById(R.id.tv_reply_to);
    }

    private void initData() {
        if (!StringUtils.isEmpty(text)) {
            tvReplyTo.setText(text);
        }
        etFragmentComment.requestFocus();//设置焦点
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);//设置输入盘可见
        btnFragmentComment.setOnClickListener(v -> {
            comment();
        });
    }

    private void comment() {
        String str = etFragmentComment.getText().toString().trim();
        if (StringUtils.isEmpty(str)) {
            showToast("评论不能为空！");
            return;
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put("content", str);
        params.put("classroomId", clsId);
        params.put("parentId", parentId);
        Log.d("TAG", "comment: " + clsId + " pid:" + parentId);
        Api.config(ApiConfig.COMMENT, params).postRequest(rootView.getContext(), new ApiCallback() {
            @Override
            public void onSuccess(String res) {
                Log.d("TAG", "onSuccess: ");
                RespData<CommentDTO> respData = StringUtils.fromJson(res, new TypeToken<RespData<CommentDTO>>(){}.getType());
                if (respData.getCode() == 200) {
                    CommentDTO commentDTO = respData.getData();
                    dismiss();
                    if (commentListener != null) commentListener.onSuccess(commentDTO);
                } else {
                    if (commentListener != null) commentListener.onFail();
                    showToastSync("服务器异常，请重试！");
                }
            }

            @Override
            public void onFailure(Exception e) {
                showToastSync("网络异常，请稍等！");
            }
        });
    }

    private void showToastSync(String msg) {
        Looper.prepare();
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
        Looper.loop();
    }

    private void showToast(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    public void setCommentListener(CommentListener listener) {
        this.commentListener = listener;
    }
}
